//
//  OYCVisitorView.swift
//  WeiBo-Swift
//
//  Created by daniel on 2017/7/26.
//  Copyright © 2017年 daniel. All rights reserved.
//

import UIKit

protocol OYCVisitorViewDelegate {
    
    func registerBtnDidClick()
    func loginBtnDidClick()
}

class OYCVisitorView: UIView {
    
    var delegate: OYCVisitorViewDelegate?
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var backIconImageView: UIImageView!
    @IBOutlet weak var maskImageView: UIImageView!
    
    @IBAction func registerClick() {
        
        guard delegate == nil else {
            
            delegate!.registerBtnDidClick()
            return
        }
        
    }
    
    @IBAction func loginClick(_ sender: UIButton) {
        guard delegate == nil else {
            
            delegate!.loginBtnDidClick()
            return
        }
    }
    class func visitorView(title: String?, iconName: String?, isHome: Bool, delegate: OYCVisitorViewDelegate ) -> OYCVisitorView {
        
        let _instance: OYCVisitorView
        
        _instance = Bundle.main.loadNibNamed("OYCVisitorView", owner: nil, options: nil)?.last as! OYCVisitorView
        _instance.delegate = delegate
        if isHome {
            let animation = CABasicAnimation()
            animation.keyPath = "transform.rotation"
            animation.toValue = 2 * Double.pi
            animation.duration = 20
            animation.repeatCount = MAXFLOAT
            animation.isRemovedOnCompletion = false
            _instance.backIconImageView.layer.add(animation, forKey: "rotation")
        }else {
            _instance.maskImageView.isHidden = true
            _instance.backIconImageView.isHidden = true
            _instance.iconImageView.image = UIImage(named: iconName ?? "")
            _instance.titleLabel.text = title
        }
        
        return _instance
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }

}
